<?php

/*
 * Edit by xinli
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */

namespace api\mall\controller;

use app\user\model\UserModel;
use api\mall\model\OnlineModel;
use app\portal\model\UserPromoteRelationModel;
use cmf\controller\RestBaseController;

/**
 * @OA\Tag(
 *     name="mall-ranking",
 *     description="排行"
 * )
 */
class RankingController extends RestBaseController
{
    protected function initialize()
    {
        parent::initialize();
        if (empty($this->user)) {
            $this->error('login');
        }
        online_record($this->userId);
    }

    /**
     * 获取排行列表
     * @OA\Get(
     *     tags={"mall-ranking"},
     *     path="/mall/ranking/list",
     *     @OA\Parameter(name="type",in="query",description="排行类型 1活跃度 2召唤好友 3富豪榜单",required=true,@OA\Schema(type="integer")),
     *     @OA\Response(response=200, ref="#/components/responses/200")
     * )
     */
    public function list()
    {
        $type = $this->request->param('type', 1, 'intval');

        $rankingConfig = cmf_get_option('ranking_list');

        $rankingList = [];
        switch ($type) {
            case 1:
                $model = new OnlineModel();
                $rankingList = $model->alias('o')
                    ->leftJoin('user u', 'o.user_id = u.id')
                    ->field('u.id, u.user_nickname, u.avatar, sum(o.total_time) as total_time')
                    ->group('o.user_id')
                    ->order('total_time desc')
                    ->limit(isset($rankingConfig['activity_count']) && !empty($rankingConfig['activity_count']) ? $rankingConfig['activity_count'] : 50)
                    ->select();
                break;
            case 2:
                $userPromoteRelationModel = new UserPromoteRelationModel();
                $rankingList = $userPromoteRelationModel->alias('upr')
                    ->leftJoin('user u', 'upr.user_id = u.id')
                    ->field('u.id, u.user_nickname, u.avatar, upr.invite_size')
                    ->order('invite_size desc')
                    ->limit(isset($rankingConfig['invite_count']) && !empty($rankingConfig['invite_count']) ? $rankingConfig['invite_count'] : 50)
                    ->select();
                break;
            case 3:
                // $rankingList = Db::name('user_score_log')
                //     ->alias('usl')
                //     ->leftJoin('user u', 'usl.user_id = u.id')
                //     ->field('u.id, u.user_nickname, u.avatar, sum(usl.score) as total_score')
                //     ->where('usl.score', '<', '0')
                //     ->group('usl.user_id')
                //     ->order('total_score asc')
                //     ->limit(isset($rankingConfig['cost_count']) && !empty($rankingConfig['cost_count']) ? $rankingConfig['cost_count'] : 50)
                //     ->select()
                //     ->toArray();

                $model = new UserModel();
                $query = $model->alias('u');
                $query->leftJoin('cmf_store_log csl', 'csl.user_id = u.id');
                $rankingList = $query->field('u.id, u.user_nickname, u.avatar, COALESCE(SUM(csl.cost), 0) AS total_score')
                    ->group('u.id')
                    ->order('total_score desc')
                    ->order('id asc')
                    ->limit(isset($rankingConfig['cost_count']) && !empty($rankingConfig['cost_count']) ? $rankingConfig['cost_count'] : 50)
                    ->select()
                    ->toArray();
                break;
        }

        foreach ($rankingList as &$item) {
            $item['avatar'] = cmf_get_image_url($item['avatar']);
        }
        $this->success('ok', $rankingList);
    }
}
